package com.sun.hunk.struts.action;

import com.sun.hunk.hibernate.DAOFactory;
import com.sun.hunk.hibernate.domain.Category;
import com.sun.hunk.struts.HunkDispatchAction;
import com.sun.hunk.struts.form.CategoryForm ;
import com.sun.hunk.util.CommonUtil;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author sunil
 */
public class CategoryAction extends HunkDispatchAction {

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CategoryForm  CategoryForm  = (CategoryForm ) form;

        if (!"Search".equals(CategoryForm .getButton())) {
            Category catagory = CategoryForm .getCategory();
            String error = validate(catagory);
            if (error != null) {
                setError(request, error);
//            return mapping.findForward(SUCCESS);
            } else {
                DAOFactory.getCategoryDAO().saveOrUpdate(catagory);               
                setMessage(request, "Category saved successfully.");
            }
        }
        search(mapping, form, request, response);
        clear(mapping, form, request, response);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CategoryForm  CategoryForm  = (CategoryForm ) form;
        Long id = CategoryForm .getId();
        Category categarory = DAOFactory.getCategoryDAO().findById(id);
        CategoryForm .setCategory(categarory);
        request.setAttribute("categoryForm ", CategoryForm );
        search(mapping, form, request, response);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward geSubcategories(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CategoryForm  CategoryForm  = (CategoryForm ) form;
        Long id = CategoryForm .getId();
        Category category = DAOFactory.getCategoryDAO().findById(id);
        request.setAttribute("category", category);
        return mapping.findForward("subcategory");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {        
        List<Category> list = DAOFactory.getCategoryDAO().findAllParent();
        request.setAttribute("categories", list);
        return mapping.findForward(SUCCESS);
    }

    public String validate(Category category) {
        if (CommonUtil.isEmpty(category.getDescription())) {
            return "Category should not be empty";
        } else if (category.getId() == null && DAOFactory.getCategoryDAO().isCategoryExist(category.getDescription())) {
            return "Category already exists.";
        } else {
            return null;
        }
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CategoryForm  CategoryForm  = (CategoryForm ) form;
        DAOFactory.getCategoryDAO().delete(CategoryForm .getCategory());
        search(mapping, form, request, response);
        setMessage(request, "Deleted successfully");
        request.setAttribute("CategoryForm ", new CategoryForm ());
        return mapping.findForward(SUCCESS);
    }
    
    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.removeAttribute("categoryForm");
        return mapping.findForward(SUCCESS);
    }
}
